package com.baidu.cloudmusic

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.ConnectivityManager.OnNetworkActiveListener
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import com.baidu.cloudmusic.databinding.ActivitySplashBinding
import com.baidu.lib_common.base.BaseActivity
import com.baidu.lib_common.constant.NetWorkAvailableEvent
import com.baidu.lib_common.constant.NetWorkLostEvent
import com.baidu.lib_common.utils.loadurl
import com.baidu.lib_home.activity.MainActivity
import com.baidu.lib_leancloud.LeanCloudManager
import com.baidu.lib_leancloud.db.RoomHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class SplashActivity : BaseActivity<ActivitySplashBinding>() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
    private var time = 5

    private lateinit var timeJob: Job
    override fun initUI() {
        super.initUI()
        initClickEvent()
//        LeanCloudManager.instance.loadChart{leanCloudManager, charts ->
//            leanCloudManager.loadMusic()
//        }
//        val startTime = System.currentTimeMillis()
//        LeanCloudManager.instance.loadAdv{leanCloudManager, advertisements ->
//            mBinding.advertisement.loadurl(leanCloudManager.getRandomAdv().image)
//            val endTime = System.currentTimeMillis()
//            if(endTime - startTime < 1500){
//                lifecycleScope.launch{
//                    delay(1500 - (endTime - startTime))
//                    showAdvertisement()
//                }
//            }else{
//                showAdvertisement()
//            }
//        }
        RoomHelper.initViewModel(this)


    }
    fun goToNextPage(){
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }
    fun showAdvertisement(){
        mBinding.splash.visibility = View.GONE
        timeJob = lifecycleScope.launch(Dispatchers.IO){
            while (true){
                delay(1000)
                time--
                if (time == 0){
                    goToNextPage()
                    timeJob.cancel(null)
                    break
                }else{
                    withContext(Dispatchers.Main){
                        mBinding.skipBtn.text = "跳过 $time"
                    }
                }
            }
        }
    }
    fun initClickEvent(){
        mBinding.skipBtn.setOnClickListener{
            goToNextPage()
        }
    }
    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    fun OnNetworkAvailableEvent(event: NetWorkAvailableEvent){
        LeanCloudManager.instance.changeIsNetWork(true)
        LeanCloudManager.instance.loadChart{leanCloudManager, charts ->
            leanCloudManager.loadMusic()
        }
        val startTime = System.currentTimeMillis()
        LeanCloudManager.instance.loadAdv{leanCloudManager, advertisements ->
            mBinding.advertisement.loadurl(leanCloudManager.getRandomAdv().image)
            val endTime = System.currentTimeMillis()
            if(endTime - startTime < 1500){
                lifecycleScope.launch{
                    delay(1500 - (endTime - startTime))
                    showAdvertisement()
                }
            }else{
                showAdvertisement()
            }
        }

    }
    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    fun OnNetWorkLostEvent(event: NetWorkLostEvent){
        LeanCloudManager.instance.changeIsNetWork(false)
        Toast.makeText(this,"请检查网络设置,需要网络才能正常运行应用",Toast.LENGTH_LONG).show()
    }

}